from django.db import models
from django.contrib.auth.models import AbstractUser, Group, Permission
from django.utils.translation import gettext_lazy as _


# 自定义用户模型
class User(AbstractUser):
    role = models.CharField(max_length=20, choices=[('admin', 'Admin'), ('manager', '经理'), ('user', '用户')])

    groups = models.ManyToManyField(
        Group,
        verbose_name=_('groups'),
        blank=True,
        help_text=_('The groups this user belongs to.'),
        related_name='user_groups',  # 添加 related_name
        related_query_name='user_groups',
    )

    user_permissions = models.ManyToManyField(
        Permission,
        verbose_name=_('user permissions'),
        blank=True,
        help_text=_('Specific permissions for this user.'),
        related_name='user_perms',  # 添加 related_name
        related_query_name='user_perms',
    )

# 货物表
class Goods(models.Model):
    goods_name = models.CharField(max_length=100)
    description = models.TextField(blank=True, null=True)
    unit = models.CharField(max_length=20)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    image_url = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    on_shelves = models.BooleanField(default=False)  # 商品是否已上架
    def __str__(self):
        return self.goods_name

# 库存表
class Inventory(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE)
    min_quantity_alert = models.IntegerField()
    last_updated = models.DateTimeField(auto_now=True)
    current_quantity = models.IntegerField(default=0)  # 设置默认值为 0
    total_inventory = models.IntegerField(default=0) # 总库存量（仓库 + 货架）


    def __str__(self):
        return f"库存：{self.goods.goods_name}"

# 客户表
class Customer(models.Model):
    customer_name = models.CharField(max_length=100)
    contact = models.CharField(max_length=50)
    address = models.CharField(max_length=255, blank=True, null=True)
    email = models.EmailField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.customer_name

# 销售订单表
class SalesOrder(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    order_date = models.DateTimeField()
    total_amount = models.DecimalField(max_digits=10, decimal_places=2)
    status = models.CharField(max_length=20, choices=[('pending', '待处理'), ('shipped', '已发货'), ('completed', '已完成')])
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"销售订单：{self.customer.customer_name}"

# 销售明细表
class SaleDetail(models.Model):
    order = models.ForeignKey(SalesOrder, on_delete=models.CASCADE)
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE)
    quantity = models.IntegerField()
    unit_price = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return f"销售明细：订单编号 {self.order.id}，货物 {self.goods.goods_name}"

# 报表表
class Report(models.Model):
    REPORT_TYPES = [('sales', '销售报表'), ('inventory', '库存报表')]
    REPORT_STATUS = [('generated', '已生成'), ('pending', '待处理')]
    report_type = models.CharField(max_length=10, choices=REPORT_TYPES)
    report_date = models.DateTimeField()
    generated_by = models.ForeignKey(User, on_delete=models.CASCADE)
    file_path = models.CharField(max_length=255, blank=True, null=True)
    status = models.CharField(max_length=10, choices=REPORT_STATUS)

    def __str__(self):
        report_type_display = dict(self.REPORT_TYPES).get(self.report_type, '未知类型')
        report_status_display = dict(self.REPORT_STATUS).get(self.status, '未知状态')
        return f"{report_type_display} - {report_status_display}（{self.report_date}）"